package com.gjy.disruptor.longevent;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-04-02 12:24:39
 */
public class LongEventMain {

    public static void main(String[] args) throws Exception {
        int bufferSize = 1024; // 环形缓冲区大小，必须是2的幂

        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, new DisruptorThreadFactory());

        disruptor.handleEventsWith(new LongEventHandler());
        disruptor.start();


        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) {
            bb.putLong(0, l);
            // ringBuffer.publishEvent((event, sequence, buffer) -> event.set(buffer.getLong(0)), bb);
            ringBuffer.publishEvent(new LongEventTranslator());
            ringBuffer.publishEvent(new LongEventTranslator());
            TimeUnit.SECONDS.sleep(5L);
        }
    }

}
